<?php

class DBProcessor_DBElements  {
	
	const DBQUERY_SELECT = 'SELECT ';
	const DBQUERY_INSERT = 'INSERT ';
	const DBQUERY_UPDATE = 'UPDATE ';
	const DBQUERY_DELETE = 'DELETE FROM ';
	const DBQUERY_FROM = 'FROM';
	const DBQUERY_SET = 'SET';
	const DBQUERY_WHERE = 'WHERE ';
	const DBQUERY_AND = 'AND ';
	
	
	/**
	 * 
	 * This function creates an SQL SELECT-QUERY and fetches the data from the corresponding
	 * table. Afterwards an associative array will be returned
	 * @param array $columns
	 * @param String $table
	 * @param String $condition
	 */
	public static function fetchData ($columns, $table, $condition)  {
		
// Check if columns and a table has been provided.
		if (!empty($columns) && !empty($table))  {
			$query = self::DBQUERY_SELECT;
			
			$firstColumn = true;
			foreach ($columns as $column)  {
				
// If the first column or * is processed don't append a comma before
				if ($firstColumn)  {
					if ($column == '*')  {
						$query .= $column;
					}
					else  {
						$firstColumn = false;
						$query .= $column;
					}
				}
				
// After the first run of foreach append always firstly a comma before appending
// the current column.
				else  {
					$query .= ', '. $column;
				}
			}
			
			$query .= ' '. self::DBQUERY_FROM. ' '. $table;

		}

// If query is empty don't append the condition because an invalid query would be generated
		if (!empty($condition) && !empty($query))  {
			$query .= ' '. self::DBQUERY_WHERE. ' '. $condition;
		}
		
// If query is not empty and columns, table and condition has been transferred into a valid
// SQL query run the query and return the data.
		if (!empty($query))  {

			$query = mysql_query($query);
			
			$subscribers['ColumnNames'] = self::getColumnNames($query);
			
			while ($subscriber = mysql_fetch_assoc($query))  {
				$subscribers[] = $subscriber;
			}
			
			return $subscribers;
		}
		else  {
			return false;
		}
		
	}
	
	public static function getColumnNames ($query)  {
		$columns = array();
		
		$countColumns = mysql_num_fields($query);

		for ($i = 0; $i < $countColumns; $i++)  {
			$columns[] = mysql_field_name($query, $i);
		}
		
		return $columns;
	}
	
	/**
	 * 
	 * This function creates an SQL UPDATE-QUERY and updates the data in the corresponding
	 * table.
	 * @param String $table
	 * @param array $columnsToChange
	 * @param String $condition
	 */
	public static function updateData ($table, $columnsToChange, $condition)  {
		
		$result = false;
		
		if (!empty($table) && !empty($columnsToChange))  {
			$query = self::DBQUERY_UPDATE;
			
			$query .= ' '. $table;
			
			$query .= ' '. self::DBQUERY_SET. ' ';
			
			foreach ($columnsToChange as $column=>$data)  {
				$query .= $column. ' = '. $data. ' ';
			}
		}
		
		if (!empty($query) && !empty($condition))  {
			$query .= $condition;
		}
		
		if (!empty($query))  {
			
			$result = mysql_query($query);
		}
		
		return $result;
	}
	
	
	public static function deleteData ($table, $condition)  {
		
		if (!empty($table))  {
		
			$query = self::DBQUERY_DELETE;
			
			$query .= $table;
			
		}
		
		if (!empty($query) && !empty($condition))  {
			
			$query .= ' '. $condition;
			
		}
		
		if (!empty($query))  {
			
			$result = mysql_query($query);
			
		}
		
		return $result;
		
		
	}
	
	public static function andConcat ($condition)  {
		$query = self::DBQUERY_WHERE. ' ';
		
		foreach ($condition as $column=>$data)  {
			if (is_array($data))  {
				foreach ($data as $value)  {
					$query .= $column. ' = '. $value. ' ';
				}
			}
			else  {
				$query .= $column. ' = '. $data;
			}
		}
		
		return $query;
	}
	
}